package com.example.sprintboot.service;

import com.example.sprintboot.entity.Employee;
import com.example.sprintboot.mapper.EmployeeMapper; // 假设存在 EmployeeMapper 接口
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper; // 注入 EmployeeMapper


    public List<Employee> selectAll() {
        return employeeMapper.selectAll(); // 调用 EmployeeMapper 的方法
    }

    public Employee selectById(Integer id) {
        return employeeMapper.selectById(id);
    }

    public void add(Employee employee) {
        String username= employee.getUsername();
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if (dbEmployee == null) {
            employeeMapper.insert(employee);
        }
    }

    public void update(Employee employee) {
        employeeMapper.updateById(employee);
    }

    public void deleteById(Integer id) {
        employeeMapper.deleteById(id);
    }


    public Employee login(Employee employee) {
       String username= employee.getUsername();
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if (dbEmployee == null) {
            return null;
        }
       String password= employee.getPassword();
        if (!dbEmployee.getPassword().equals(password)) {
            return null;
        }
        return dbEmployee;

    }
}
